/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.zhpan.bannerviewpager.slice;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderIndicator;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;

import com.example.zhpan.bannerviewpager.ResourceTable;
import com.example.zhpan.bannerviewpager.adapter.PageProvider;

import com.zhpan.bannerview.Log;
import com.zhpan.bannerview.view.AccordionTransformer;
import com.zhpan.bannerview.view.CollPageSlide;
import com.zhpan.bannerview.view.CubeInTransformer;

/**
 * SplashScreenAbilitySlice
 *
 * @since 2021-03-24
 */
public class SplashScreenAbilitySlice extends Ability {
    private static final float MIN_SCALE = 0.75f;
    private static final int ONEZEROZERO = 100;
    private static final int THREE = 3;
    private static final int TWO = 2;
    private static final int ONE = 1;
    private static final int TEN = 10;
    private static final int TWOZERO = 20;
    private static final int FFINT = 0xff;
    private static final float ZEROTWO = 0.2f;
    private static final int THREEZERO = 30;
    private static final String HERE = "在这里";
    private CollPageSlide mPageSlider;
    private Button btnStart;
    private Text tvDescribe;
    private Text tvDescribe1;
    private PageSliderIndicator indicator;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_splash_screen_layout);
        initView();
        initPages();
    }

    /**
     * initPages
     */
    private void initPages() {
        mPageSlider = (CollPageSlide) findComponentById(ResourceTable.Id_page_slide);
        PageProvider pageProvider = new PageProvider(getContext());
        mPageSlider.setProvider(pageProvider);
        mPageSlider.setPages(pageProvider.getComponents());
        int random = (int) (Math.random() * ONEZEROZERO % THREE + 1);
        try {
            if (random == 1) {
                mPageSlider.setPageTransformer(ScaleInOutTransformer.class.newInstance());
            } else if (random == TWO) {
                mPageSlider.setPageTransformer(CubeInTransformer.class.newInstance());
            } else if (random == THREE) {
                mPageSlider.setPageTransformer(AccordionTransformer.class.newInstance());
            }
        } catch (IllegalAccessException e) {
            Log.debug(e.getMessage());
        } catch (InstantiationException e) {
            Log.debug(e.getMessage());
        }
        indicator = (PageSliderIndicator) findComponentById(ResourceTable.Id_indicator);
        ShapeElement shape = new ShapeElement();
        shape.setCornerRadius(AttrHelper.vp2px(TWOZERO, getContext()));
        shape.setRgbColor(new RgbColor(FFINT, FFINT, FFINT)); // 设置颜色,白色
        ShapeElement shape1 = new ShapeElement();
        shape1.setCornerRadius(AttrHelper.vp2px(TWOZERO, getContext()));
        shape1.setRgbColor(new RgbColor(FFINT, 0x00, 0x00)); // 红色
        indicator.setItemElement(shape, shape1);
        indicator.setItemOffset(AttrHelper.vp2px(TWOZERO, getContext()));
        indicator.setScale(ZEROTWO, ZEROTWO);
        indicator.setPageSlider(mPageSlider); // 将指示器和pageslider绑定
        pageSetOnClick();
    }

    /**
     * pageSetOnClick
     */
    private void pageSetOnClick() {
        mPageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i3, float v1, int i1) {
                setTrantion(tvDescribe, tvDescribe1, v1);
            }

            /**
             * onPageSlideStateChanged
             *
             * @param i2 参数
             */
            @Override
            public void onPageSlideStateChanged(int i2) {
            }

            /**
             * onPageChosen
             *
             * @param i1 参数
             */
            @Override
            public void onPageChosen(int i1) {
                if (i1 == 0) {
                    tvDescribe.setText(HERE);
                    tvDescribe1.setText("你可以听到周围人的心声");
                }
                if (i1 == 1) {
                    tvDescribe.setText(HERE);
                    tvDescribe1.setText("TA会在下一秒遇见你");
                }
                if (i1 == TWO) {
                    tvDescribe.setText(HERE);
                    tvDescribe1.setText("不再错过可以改变你一生的人");
                    btnStart.setVisibility(Component.VISIBLE);
                } else {
                    btnStart.setVisibility(Component.HIDE);
                }
            }
        });
    }

    /**
     * initView
     */
    private void initView() {
        btnStart = (Button) findComponentById(ResourceTable.Id_btn_start);
        tvDescribe = (Text) findComponentById(ResourceTable.Id_tv_describe);
        tvDescribe1 = (Text) findComponentById(ResourceTable.Id_tv_describe1);
        btnStart.setVisibility(Component.HIDE);
        tvDescribe.setText(HERE);
        tvDescribe1.setText("你可以听到周围人的心声");
        btnStart.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                    .withBundleName(getContext().getBundleName())
                    .withAbilityName("com.example.zhpan.bannerviewpager.MainAbility")
                    .build();
                intent.setOperation(operation);
                startAbility(intent, TEN);
                terminateAbility();
            }
        });
    }

    /**
     * textView 的滑动时间
     *
     * @param test 參數
     * @param test1 參數
     * @param postion 參數
     */
    private void setTrantion(Text test, Text test1, float postion) {
        test.setAlpha(postion);
        test1.setAlpha(postion);
        test.setTranslationX(AttrHelper.vp2px(THREEZERO * postion, getContext()));
        test1.setTranslationX(AttrHelper.vp2px(THREEZERO * postion, getContext()));
    }
}
